import {createSlice,createAsyncThunk} from "@reduxjs/toolkit";
import { getBanners,getHotRecommend,getNewAlbums,getTopRankings,getSettleSingerList } from "../service";

export const fetchBannerDataAction = createAsyncThunk("banners",async (arg:any,{getState,dispatch})=>{
    // console.log(arg,getState,dispatch,"=====");
    const res = await getBanners();
    // console.log(res);
    dispatch(changeBannerAction(res.banners));
});
export const fetchHotRecommendDataAction = createAsyncThunk("hotRecommend",async (arg,{dispatch})=>{
    const res = await getHotRecommend(8);
    dispatch(changeHotRecommendAction(res.result));
    // console.log(res);
});
export const fecthNewAlbumsDataAction = createAsyncThunk("newAlbums",async (arg,{dispatch})=>{
    const res = await getNewAlbums();
    // console.log(res);
    dispatch(changeNewAlbumsAction(res.albums));
});
export const fecthTopRankingsDataAction = createAsyncThunk("topRanking",async (arg,{dispatch})=>{
    const rankingIds = [19723756,3779629,2884035];
    const promises:Promise<any>[] = [];
    for(const id of rankingIds){
        promises.push(getTopRankings(id));
    }
    // console.log(promises);
    Promise.all(promises).then((res)=>{
        // console.log(res);
        const playlists = res.map((item) => item.playlist);
        dispatch(changeTopRankingsAction(playlists));
    });
});
export const fecthSettleSingerDataAction = createAsyncThunk("settleSinger",async (arg,{dispatch})=>{
    const res = await getSettleSingerList(5);
    // console.log(res);
    dispatch(changeSettleSingerAction(res.artists));
});

interface IRecommand {
    banners:any[]
    hotRecommend:any[]
    newAlbums:any[]
    rankings:any[]
    settleSinger:any[]
}

const initialState:IRecommand = {
    banners:[],
    hotRecommend:[],
    newAlbums:[],
    rankings:[],
    settleSinger:[]
}

const recommandSlice = createSlice({
    name:"recommand",
    initialState,
    reducers:{
        changeBannerAction(state,action){
            state.banners = action.payload;
        },
        changeHotRecommendAction(state,action){
            state.hotRecommend = action.payload;
        },
        changeNewAlbumsAction(state,action){
            state.newAlbums = action.payload;
        },
        changeTopRankingsAction(state,action){
            state.rankings = action.payload;
        },
        changeSettleSingerAction(state,action){
            state.settleSinger = action.payload;
        }
    },
    // extraReducers:(builder)=>{
    //     builder.addCase(fetchBannerDataAction.pending,(state,action)=>{
    //         console.log("pending",state,action);
    //     }).addCase(fetchBannerDataAction.fulfilled,(state,action)=>{
    //         console.log("fulfilled",state,action);
    //         state.banners = action.payload;
    //     }).addCase(fetchBannerDataAction.rejected,(state,action)=>{
    //         console.log("rejected",state,action);
    //     });
    // }
});

export const {
    changeBannerAction,
    changeHotRecommendAction,
    changeNewAlbumsAction,
    changeTopRankingsAction,
    changeSettleSingerAction
} = recommandSlice.actions;
export default recommandSlice.reducer;
